<template>
  <div>
     <div>
      <h1>日志管理</h1>
    </div>
    <el-form :inline="true">
      <el-row>
        <el-col :span="5">
          <el-form-item label="管理员账号：">
            <el-input
              v-model="selectData.account"
              placeholder="请输入管理员账号"
              style="width: 240px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="操作：">
            <el-input
              v-model="selectData.operation"
              placeholder="请输入操作"
              style="width: 240px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="方法：">
            <el-input
              v-model="selectData.method"
              placeholder="请输入方法"
              style="width: 240px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="时间：">
            <el-date-picker
              v-model="data"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 240px"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="getLogHandler">查询</el-button>
          <el-button type="primary" @click="resetHandler">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" v-loading="dataLoading" element-loading-text="拼命加载中">
      <el-table-column prop="adminDO.account" label="管理员" align="center">
      </el-table-column>
      <el-table-column prop="operation" label="操作" align="center">
      </el-table-column>
      <el-table-column prop="method" label="方法" align="center">
      </el-table-column>
      <el-table-column prop="operateTime" label="操作时间" align="center">
      </el-table-column>
      <el-table-column prop="serach" label="查看" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="getOperateDataHandler(scope.row)"
            >查看操作数据</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="selectData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="selectData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getLog } from "@/api/log";
export default {
  data() {
    return {
      //加载状态
      dataLoading:false,
      data: [],
      selectData: {
        account: null,
        operation: null,
        method: null,
        startTime: null,
        stopTime: null,
        currentPage: 1, //当前页
        pageSize: 5, //每页大小
      },
      total: 0,
      tableData: [],
    };
  },
  methods: {
    //查看操作数据
    getOperateDataHandler(row) {
      this.$confirm(
        `账号为${row.adminDO.account}的管理员在${row.operateTime}调用了${row.method}方法进行了${row.operation}操作`,
        {
          title: "操作详情",
          showConfirmButton: false, 
          cancelButtonText: "关闭", 
        }
      ).catch(() => {});
    },
    //重置
    resetHandler() {
      this.selectData.account = null;
      this.selectData.operation = null;
      this.selectData.method = null;
      this.selectData.startTime = null;
      this.selectData.stopTime = null;
      this.data = [];
      this.getLogHandler();
    },
    //查询
    getLogHandler() {
      //开启加载动画
      this.dataLoading=true;
      getLog(this.selectData)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.selectData.currentPage = res.data.pageNum
          //关闭加载动画
          this.dataLoading=false;
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.getLogHandler();
    },
    handleCurrentChange(val) {
      this.selectData.currentPage = val;
      this.getLogHandler();
    },
  },
  mounted() {
    this.getLogHandler();
  },
  watch: {
    // 监听 selectData.data 数组变化，自动拆分到 startDate 和 endDate
    data(newVal) {
      if (newVal && newVal.length === 2) {
        this.selectData.startTime = newVal[0];
        this.selectData.stopTime = newVal[1];
      } else {
        this.selectData.startTime = null;
        this.selectData.stopTime = null;
      }
    },
  },
};
</script>

<style>
</style>